package com.xcity.game.achievement.helper;

import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;
import com.xcity.game.achievement.AchievementTemplate;

/**
 * 成就类型：消耗现金<br/>
 * 1个参数：①完成该成就需消耗的现金数量
 * @author yang.li
 *
 */
public class GoldCostHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		if (refer.getState() == State.REWARDED) {
			return false;
		}
		String key = DEFAULT_VAL_KEY;
		long val = refer.getValue().getLongValue(key) + obtainValue(vals);
		refer.getValue().put(key, val);
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() == State.INITED
				&& refer.getValue().getLongValue(DEFAULT_VAL_KEY) >= obtainValue(refer.getTemplate())) {
			refer.setState(State.FINISHED);
			return true;
		}
		return false;
	}
	
	static long obtainValue(AchievementTemplate template) {
		return template.getCondition(0);
	}
	
	static int obtainValue(int... vals) {
		return vals != null && vals.length > 0 ? vals[0] : 0;
	}

}
